program drop _all
program define TABLE8prog, rclass
version 14
syntax, beta0(real) betax(real) betay(real) truLRP(real) numN(integer) numT(integer) ///
    rhox(real)
drop _all


// Create the data
set obs `numN'
gen id = _n
// "ai" is the part of the error term that doesn't change over time for a given
// unit.
gen ai = rnormal()
expand `numT'
bysort id: gen t=_n
xtset id t
gen x = 0
replace x = `rhox'*L.x + rnormal() if t > 1
// "uit" is the part of the error term that varies across time
gen uit = rnormal()
// The total error term is the sum of "a" and "e"
gen error = ai + uit
gen y = (`beta0')/(1-`betay')
replace y = `beta0' + `betax'*x + `betay'*L.y + error if t > 1
gen Ly = y
replace Ly = L.y if t > 1

////////////////////////////////////////////////////
// This section calculates T=25 results
////////////////////////////////////////////////////

// AH (AH)
xtivreg y x (L.y = L(2/3).y) if t > 15, fd
return scalar AHLRP25 = _b[D.x]/(1-_b[LD.y])
testnl _b[D.x]/(1-_b[LD.y])= `truLRP'
return scalar pAHLRP25 = r(p)

// Difference GMM (DGMM)
xtabond y x if t > 15
return scalar DGMMLRP25 = _b[x]/(1-_b[L.y])
testnl _b[x]/(1-_b[L.y])=`truLRP'
return scalar pDGMMLRP25 = r(p)

// System GMM (SGMM)
xtdpdsys  y x if t > 15
return scalar SGMMLRP25 = _b[x]/(1-_b[L.y])
testnl _b[x]/(1-_b[L.y])=`truLRP'
return scalar pSGMMLRP25 = r(p)

// Mean Group from an ARDL model (MG1)
xtmg y x Ly  if t > 15 
return scalar MG1LRP25 = _b[x]/(1-_b[Ly])
testnl _b[x]/(1-_b[Ly])=  `truLRP'
return scalar pMG1LRP25 = r(p)

// Common Correlated Effects MG (CCEMG)
xtmg y x Ly  if t > 15, cce
return scalar CCEMGLRP25 = _b[x]/(1-_b[Ly])
testnl _b[x]/(1-_b[Ly])=  `truLRP'
return scalar pCCEMGLRP25 = r(p)

// Augmented MG (AMG)
xtmg y x Ly  if t > 15 , aug
return scalar AMGLRP25 = _b[x]/(1-_b[Ly])
testnl _b[x]/(1-_b[Ly])=  `truLRP'
return scalar pAMGLRP25 = r(p)

// Dynamic Fixed Effects (DFE)
xtpmg d.y if t > 15, lr(l.y x) ec(ecdfe) dfe
return scalar DFELRP25 = [ecdfe]x
test [ecdfe]x = `truLRP'
return scalar pDFELRP25 = r(p)

// Mean Group from a DPD model (MG2)
xtpmg d.y  if t > 15, lr(l.y x) mg ec(ecmg2a)
return scalar MG2LRP25 = [ecmg2a]x
test [ecmg2a]x = `truLRP'
return scalar pMG2LRP25 = r(p)

// Panel DOLS (DOLS)
xtdolshm y x if t > 15
return scalar DOLSLRP25 = _b[x]
test _b[x] == `truLRP'
return scalar pDOLSLRP25 = r(p)

////////////////////////////////////////////////////
// This section does the 5-year data averaging (T=5)
////////////////////////////////////////////////////

gen xbar = (x + L.x + L2.x + L3.x + L4.x)/5 if t > 4
gen ybar = (y + L.y + L2.y + L3.y + L4.y)/5 if t > 4
gen Lybar = (Ly + L.Ly + L2.Ly + L3.Ly + L4.Ly)/5 if t > 4

gen tt = 0
replace tt = 1 if t>=1 & t<=5
replace tt = 2 if t>=6 & t<=10
replace tt = 3 if t>=11 & t<=15
replace tt = 4 if t>=16 & t<=20
replace tt = 5 if t>=21 & t<=25
replace tt = 6 if t>=26 & t<=30
replace tt = 7 if t>=31 & t<=35
replace tt = 8 if t>=36 & t<=40
gen tnum = t/5
keep if tnum == tt
tsset id tt

// AH (AH)
xtivreg ybar xbar (L.ybar = L(2/3).ybar) if tt > 3, fd
return scalar AHLRP5 = _b[D.xbar]/(1-_b[LD.ybar])
testnl _b[D.xbar]/(1-_b[LD.ybar])= `truLRP'
return scalar pAHLRP5 = r(p)

// Difference GMM (DGMM)
xtabond ybar xbar if tt > 3
return scalar DGMMLRP5 = _b[xbar]/(1-_b[L.ybar])
testnl _b[xbar]/(1-_b[L.ybar])= `truLRP'
return scalar pDGMMLRP5 = r(p)

// System GMM (SGMM)
xtdpdsys  ybar xbar if tt > 3
return scalar SGMMLRP5 = _b[xbar]/(1-_b[L.ybar])
testnl _b[xbar]/(1-_b[L.ybar])=`truLRP'
return scalar pSGMMLRP5 = r(p)

// Mean Group from an ARDL model (MG1)
xtmg ybar xbar Lybar  if tt > 3 
return scalar MG1LRP5 = _b[xbar]/(1-_b[Lybar])
testnl _b[xbar]/(1-_b[Lybar])=  `truLRP'
return scalar pMG1LRP5 = r(p)

// Common Correlated Effects MG (CCEMG)
xtmg ybar xbar Lybar  if tt > 3, cce
return scalar CCEMGLRP5 = _b[xbar]/(1-_b[Lybar])
testnl _b[xbar]/(1-_b[Lybar])=  `truLRP'
return scalar pCCEMGLRP5 = r(p)

// Augmented MG (AMG)
xtmg ybar xbar Lybar  if tt > 3 , aug
return scalar AMGLRP5 = _b[xbar]/(1-_b[Lybar])
testnl _b[xbar]/(1-_b[Lybar])=  `truLRP'
return scalar pAMGLRP5 = r(p)

// Dynamic Fixed Effects (DFE)
xtpmg d.ybar if tt > 3, lr(l.ybar xbar) ec(ecdfe) dfe
return scalar DFELRP5 = [ecdfe]xbar
test [ecdfe]xbar = `truLRP'
return scalar pDFELRP5 = r(p)

// Mean Group from a DPD model (MG2)
xtpmg d.ybar  if tt > 3, lr(l.ybar xbar) mg ec(ecmg2b)
return scalar MG2LRP5 = [ecmg2b]xbar
test [ecmg2b]xbar = `truLRP'
return scalar pMG2LRP5 = r(p)

end
